package ua.kiev.tosik.price.modules.clients.models;

import ua.kiev.tosik.price.gui.models.SimpleTableModel;
import ua.kiev.tosik.price.models.PricesFiles;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import javax.swing.*;
import java.util.List;

public class PricesFilesTableModel extends SimpleTableModel {

	private List<PricesFiles> data;
	private ImageIcon fileIcon;
	private ImageIcon parsedIcon;
	private ImageIcon unParsedIcon;
	private ModuleConfig config;

	public PricesFilesTableModel(List<PricesFiles> data) {
		this.data = data;
		config = ModuleConfig.getInstance();
		fileIcon = new ImageIcon(config.get("clients.files.table.icon"));
		parsedIcon = new ImageIcon(config.get("clients.files.table.parsed.icon"));
		unParsedIcon = new ImageIcon(config.get("clients.files.table.unParsed.icon"));
		colNames = new String[]{
			"[..]",
			config.get("clients.files.table.fileName"),
			config.get("clients.files.table.fileSize"),
			config.get("clients.files.table.date"),
			config.get("clients.files.table.parsed")
		};
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	public void setColNames(String[] cols) {
		this.colNames = cols;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		PricesFiles pricesFiles = data.get(rowIndex);
		switch (columnIndex) {
			case 0:
				return fileIcon;
			case 1:
				return pricesFiles.getFileName();
			case 2:
				return pricesFiles.getFileSize();
			case 3:
				return pricesFiles.getPricesDate();
			case 4:
				return (pricesFiles.isParsed())?parsedIcon:unParsedIcon;
			default:
				return "";
		}
	}

	@Override
	public Class getColumnClass(int c) {
		switch (c) {
			case 0:
				return (ImageIcon.class);
			case 4:
				return (ImageIcon.class);
			default:
				return (String.class);
		}
	}

	public void removeRow(int idx) {
		data.remove(idx);
		fireTableRowsDeleted(idx, idx);
	}

	public void addRowSet(List<PricesFiles> data) {
		this.data.clear();
		this.data.addAll(data);
		fireTableDataChanged();
		logger.debug("Added " + this.data.size() + " records to table");
	}

	public List<PricesFiles> getRowSet() {
		return this.data;
	}

	public void addRow(PricesFiles rowData) {
		this.data.add(rowData);
		fireTableDataChanged();
	}

	public void addRow(int rowIndex, PricesFiles rowData) {
		this.data.add(rowIndex, rowData);
		fireTableRowsInserted(rowIndex, rowIndex);
	}

	public void updateRow(int rowIndex, PricesFiles rowData) {
		this.data.set(rowIndex, rowData);
		fireTableRowsUpdated(rowIndex, rowIndex);
	}

	public PricesFiles getRow(int index) {
		return data.get(index);
	}

	public void clear() {
		this.data.clear();
		fireTableDataChanged();
	}

}
